
const { HabitUrl, loginUrl } = require("../../../config/config");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    notLogin:true,
    myHabits:[],
    itemList:['修改习惯图标','删除习惯']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    if(wx.getStorageSync('userinfo')){
      this.setData({
        notLogin:false
      })
    }else{
      wx.hideTabBar();
    }

  },
  bindGetUserInfo:function(e){
    let _this = this
    let currentUser = e.detail.userInfo
    wx.login({
      success: function(res){
        if(res.code){
          currentUser['code'] = res.code
          wx.request({
            url: loginUrl,
            data: currentUser,
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: {// 设置请求的 header
              'content-type': 'application/json'
            }, 
            success: function(res){
              currentUser['id'] = JSON.parse(res.data.result)
              wx.setStorage({
                data: currentUser,
                key: 'userinfo',
              })
              _this.setData({
                notLogin : false
              })
              wx.showTabBar()     
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                image: '',
                duration: 1000
              }); 
              _this.onShow()    
            }

          })
        }
      },
    })
  },
  toAddHabit:function(){
    wx.navigateTo({
      url: '/pages/habit/habitLibrary/habitLibrary',
    })
  },
  toClockIn:function(e){
    wx.navigateTo({
      url: '/pages/habit/clockin/clockin?hid='+ e.currentTarget.dataset.hid
    })
  },
  handleHabit:function(e){
    let _this = this
    let hid = e.currentTarget.dataset.hid
    wx.showActionSheet({
      itemList: ['修改习惯图标', '删除习惯'],
      success (res) {
        if(res.tapIndex === 0){
          wx.navigateTo({
            url: '/pages/habit/addHabit/addHabit?selectHabitId='+hid,
          })
        }
        if(res.tapIndex === 1){
          wx.showModal({
            title: '删除习惯',
            content: '删除习惯后，改习惯的历史记录会被清空，您确定删除？',
            confirmColor:'#FA5151',
            confirmText:'删除',
            success (res) {
              if (res.confirm) {
                wx.request({
                  url: HabitUrl+'/'+hid,
                  method: 'DELETE', 
                  success: function(){
                    wx.showToast({
                      title: '删除成功',
                      icon: 'none',
                      duration: 1000
                    });  
                    _this.onShow()
                  }
                  
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  listHabit:function(){
    let _this = this
    let uid = wx.getStorageSync('userinfo').id          
    wx.request({  //获取我的习惯
        url: HabitUrl+'/list_mine/'+uid,
         method: 'GET', 
        success: function(res){
          _this.setData({
            myHabits : res.data.result
           })
        }
    })   
  },
  onShow: function () {
    if(!this.data.notLogin){
      this.listHabit()
    }
  }
})